package 单表查询;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class Demo01 {
    public static void main(String[] args) throws Exception{
        //1.加载驱动com.mysql.jdbc.Driver
        Class.forName("com.mysql.jdbc.Driver");
        //2.使用驱动管理器（DriverManger）连接数据库
        String url = "jdbc:mysql://localhost:3306/db_jdbc_demo?characterEncoding=utf8&useSSL=false&serverTimezone=UTC";
        String user = "root";
        String pass = "1234";
        Connection conn = DriverManager.getConnection(url,user,pass); //Connection 数据库连接
        //3.书写Sql语句，并执行
        String sql = "SELECT * from t_user";
        //创建执行Sql的对象
        Statement stmt = conn.createStatement();  //Statement 执行SQL语句
        ResultSet rs = stmt.executeQuery(sql);    //ResultSet  存放结果
        //5.从rs中取出结果
        while (rs.next()){
            String username = rs.getString("username");
            String fullname = rs.getString("fullname");
            System.out.println(username+","+fullname);

        }
        //6.关闭连接
        rs.close();
        stmt.close();
        conn.close();

    }
}
